package com.feicheng.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.base.common.DaoConstant;
import com.feicheng.base.exception.SystemException;
import com.feicheng.base.result.Result;
import com.feicheng.entity.PurchaseHeader;
import com.feicheng.mapper.PurchaseHeaderMapper;
import com.feicheng.model.PurchaseHeaderWithUser;
import com.feicheng.service.IPurchaseHeaderService;
import com.feicheng.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hao.zhang
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/purchase-header")
public class PurchaseHeaderController {

    @Autowired
    PurchaseHeaderMapper purchaseHeaderMapper;

    /* 注入持久层销售头表service */
    @Autowired
    IPurchaseHeaderService iPurchaseHeaderService;

    /**
     *
     * @param current                  当前页
     * @param size                     每页条数
     * @param purchaseHeaderWithUser   业务参数（查询条件）
     * @return
     * @author feng.zhang
     */
    @PostMapping("/page/g")
    public Result selectPurchaseHeaderWithUser(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestBody PurchaseHeaderWithUser purchaseHeaderWithUser
    ){

        //构建分页
        Page<PurchaseHeaderWithUser> page = new Page<>(current,size);

        //构建动态检索条件
        QueryWrapper<PurchaseHeaderWithUser> purchaseHeaderWithUserQueryWrapper =
                new QueryWrapper<>();

        //当采购批次号不为空时
        if (StringUtils.hasLength(purchaseHeaderWithUser.getBatchNo())){

            //把采购批次号作为检索条件
            purchaseHeaderWithUserQueryWrapper.eq(DaoConstant.PURCHASE_HEADER.BATCH_NO,
                    purchaseHeaderWithUser.getBatchNo());
        }

        //当采购人姓名不为空时
        if (StringUtils.hasLength(purchaseHeaderWithUser.getUserName())){

            //把采购人姓名作为检索条件模糊查询
            purchaseHeaderWithUserQueryWrapper.like(
                    DaoConstant.MODEL.PURCHASE_HEADER_WITH_USER.USER_NAME,
                    purchaseHeaderWithUser.getUserName());
        }

        //当采购人电话不为空时
        if (StringUtils.hasLength(purchaseHeaderWithUser.getTelephone())){

            //把采购人电话号作为检索条件查询
            purchaseHeaderWithUserQueryWrapper.like(DaoConstant.MODEL.PURCHASE_HEADER_WITH_USER.TELEPHONE,
                    purchaseHeaderWithUser.getTelephone());
        }

        //当采购订单状态不为空时
        if (StringUtils.hasLength(purchaseHeaderWithUser.getState())){

            //把采购订单状态作为检索条件查询
            purchaseHeaderWithUserQueryWrapper.eq(DaoConstant.PURCHASE_HEADER.STATE,
                    purchaseHeaderWithUser.getState());
        }

        //当订单开始时间不为空时
        if (null != purchaseHeaderWithUser.getStartDate()){

            //以订单开始时间为最小时间查询
            purchaseHeaderWithUserQueryWrapper.ge(
                    DaoConstant.PURCHASE_HEADER.DATE,
                    purchaseHeaderWithUser.getStartDate());
        }

        //当订单结束时间不为空时
        if (null != purchaseHeaderWithUser.getEndDate()){

            //查询结束时间为最大时间查询
            purchaseHeaderWithUserQueryWrapper.le(
                    DaoConstant.PURCHASE_HEADER.DATE,
                    purchaseHeaderWithUser.getEndDate());
        }

        //执行查询并返回结果
        return Result.success(iPurchaseHeaderService.selectPurchaseHeader(
                page,purchaseHeaderWithUserQueryWrapper
        ));
    }

    /**
     * 修改采购订单详情状态
     * @param  purchaseHeader 订单信息
     * @autor huanning.lu
     */

    @PutMapping("/info")
    public Result updateState(@RequestBody PurchaseHeader purchaseHeader){

        //判断订单主键是否为空
        //TODO
        if ( purchaseHeader.getPurchaseHeaderId() == null ) {

            throw new SystemException("");
        }

        //获取更新前的状态
        String state = purchaseHeaderMapper.byId(purchaseHeader.getPurchaseHeaderId());

        //判断状态更新是否为逆向操作
       //TODO
        if(Integer.parseInt(state) > Integer.parseInt(purchaseHeader.getState())) {

            throw new SystemException("");
        } else{
            //返回更新结果
            return Result.success(iPurchaseHeaderService.updateById(purchaseHeader));
        }
    }
}
